package com.zjuee.web.basic.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zjuee.base.model.common.basic.Area;
import com.zjuee.web.basic.area.mapper.AreaMapper;
import com.zjuee.service.basic.area.AreaService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.*;

/**
 * @author wu liang
 * @since 1.0
 */
@Service
@Transactional(readOnly = true)
public class AreaServiceImpl extends ServiceImpl<AreaMapper, Area> implements AreaService {

    @Resource
    private AreaMapper areaMapper;


    @Override
    public List<Area> findByParentId(Long parentId) {
        LambdaQueryWrapper<Area> queryWrapper = Wrappers.<Area>query().lambda();
        Optional.ofNullable(parentId).ifPresent(x -> queryWrapper.eq(Area::getParentId, x));
        return areaMapper.selectList(queryWrapper);
    }

    @Override
    public List<Area> findAreaByLayered(Integer layered) {
        LambdaQueryWrapper<Area> queryWrapper = Wrappers.<Area>query().lambda();
        Optional.ofNullable(layered).ifPresent(x -> queryWrapper.eq(Area::getLayered, x));
        return areaMapper.selectList(queryWrapper);
    }

}
